package service

import (
	"github.com/wzh200x/hyperNexus/hyperNexusProtocol/interfaceServerProtocol"
	Server "github.com/wzh200x/webServer/server"
)

type AdOauthInfo struct {
	AppId    string `json:"appid" form:"appid"`
	Secret   string `json:"secret" form:"secret"`
	AuthCode string `json:"auth_code" form:"auth_code"`
	State    string `json:"state" form:"state"`
}

func HandleAdOauth(oauthInfo *AdOauthInfo) error {
	InterfaceClient := Server.ServerApp.InterfaceServerClient.GetClientProxy()
	var reqInfo interfaceServerProtocol.AdOauthInfo
	reqInfo.AppId = oauthInfo.AppId
	reqInfo.Secret = oauthInfo.Secret
	reqInfo.AuthCode = oauthInfo.AuthCode
	reqInfo.State = oauthInfo.State
	_, err := InterfaceClient.ByteJumpAdOauth(reqInfo)
	if err != nil {
		return err
	}
	return nil
}

type AdOauthRefreshInfo struct {
	AppId        string `json:"appid" form:"appid"`
	Secret       string `json:"secret" form:"secret"`
	RefreshToken string `json:"refresh_token" form:"refresh_token"`
}

func HadleAdOathRefresh(oauthInfo *AdOauthRefreshInfo) error {
	InterfaceClient := Server.ServerApp.InterfaceServerClient.GetClientProxy()
	var reqInfo interfaceServerProtocol.AdOauthRefreshInfo
	reqInfo.AppId = oauthInfo.AppId
	reqInfo.Secret = oauthInfo.Secret
	reqInfo.RefreshToken = oauthInfo.RefreshToken
	_, err := InterfaceClient.ByteJumpAdOauthRefresh(reqInfo)
	if err != nil {
		return err
	}
	return nil
}

type AdOauthQueryInfo struct {
	AppId string `json:"appid" form:"appid"`
}

func HandleAdOauthQuery(oauthInfo *AdOauthQueryInfo) (*interfaceServerProtocol.AdOauthQueryResponse, error) {
	InterfaceClient := Server.ServerApp.InterfaceServerClient.GetClientProxy()
	var reqInfo interfaceServerProtocol.AdOauthQueryInfo
	reqInfo.AppId = oauthInfo.AppId
	rsp, err := InterfaceClient.ByteJumpAdOauthQuery(reqInfo)
	if err != nil {
		return nil, err
	}
	return &interfaceServerProtocol.AdOauthQueryResponse{
		AppId:          rsp.AppId,
		Secret:         rsp.Secret,
		AccessToken:    rsp.AccessToken,
		RefreshToken:   rsp.RefreshToken,
		Expires:        rsp.Expires,
		ExpiresRefresh: rsp.ExpiresRefresh,
		RefreshTime:    rsp.RefreshTime,
	}, nil
}
